<template>
  <div class="app-container">
    <basic-info></basic-info>
    <safety-info @modifyPassword="modifyPassword" @modifyEmail="modifyEmail"></safety-info>
    <el-dialog
      title="修改密码"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="请输入新密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="passwordPost">提交</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改邮箱"
      :visible.sync="dialogVisible1"
      width="30%"
    >
      <el-form ref="form" :model="form1" label-width="80px">
        <el-form-item label="新邮箱">
          <el-input v-model="form1.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="emailPost">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import basicInfo from './basicInfo'
import safetyInfo from './safetyInfo'
import { modifyDoctor } from '../../../api/doctor'
import { mapGetters } from 'vuex'

export default {
  name: 'personal',
  components: {
    basicInfo,
    safetyInfo
  },
  data() {
    return {
      dialogVisible: false,
      form: {},
      form1: {},
      dialogVisible1: false
    }
  },
  methods: {
    modifyEmail() {
      this.dialogVisible1 = true
    },
    modifyPassword() {
      this.dialogVisible = true
    },
    async emailPost() {
      if (!this.form1.email) {
        this.$message({
          type: 'warning',
          message: '邮箱不能为空'
        })
        return
      }
      this.form1.id = this.userInfo.id
      const res = await modifyDoctor(this.form1)
      this.$message({
        type: 'success',
        message: res.data
      })
      this.dialogVisible1 = false
      await this.$store.dispatch('GetUserInfo')
    },
    async passwordPost() {
      if (!this.form.password) {
        this.$message({
          type: 'warning',
          message: '密码不能为空'
        })
        return
      }
      this.form.id = this.userInfo.id
      const res = await modifyDoctor(this.form)
      this.$message({
        type: 'success',
        message: res.data
      })
      this.dialogVisible = false
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
